import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:note/common/common.dart';
import 'package:note/manager/global.dart';
import 'package:note/routeConfig.dart';
import 'package:note/widgets/divider.dart';
import 'package:note/widgets/showPhotoView.dart';

class RootDrawer extends StatelessWidget {
  const RootDrawer({super.key});

  void _onBG() {
    Get.to(ShowPhotoView(GlobalUtil.appSet.value.bgImgPath ?? ''));
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Column(
        children: [
          Container(
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.zero,
            child: Obx(() {
              return GestureDetector(
                onTap: _onBG,
                child: Hero(
                  tag: 'image',
                  child: DrawerHeader(
                    decoration: BoxDecoration(
                        color: Styles.backgroundColor,
                        image: File(GlobalUtil.appSet.value.bgImgPath ?? '')
                                .existsSync()
                            ? DecorationImage(
                                image: FileImage(File(
                                    GlobalUtil.appSet.value.bgImgPath ?? '')),
                                fit: BoxFit.cover)
                            : null),
                    child: Obx(() {
                      return Text(
                        GlobalUtil.appSet.value.userName,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(
                          color: Styles.mainThemeColor,
                          fontSize: 25,
                        ),
                      );
                    }),
                  ),
                ),
              );
            }),
          ),
          listTile(Strings.mdHelper, RouteConfig.readMePage),
          divider(),
          listTile(Strings.setting, RouteConfig.settingPage),
          divider(),
          listTile(Strings.aboutApp, RouteConfig.aboutAppPage),
        ],
      ),
    );
  }
}

Widget divider() {
  return Padding(
    padding: const EdgeInsets.only(left: 10, right: 10),
    child: defaultDivider(),
  );
}

Widget listTile(String text, String route) {
  return ListTile(
    title: Text(
      text,
      style: Styles.textStyleNormal(18),
    ),
    onTap: () {
      Get.toNamed(route);
    },
  );
}
